package com.blog.blog.service.impl;

import com.blog.blog.mapper.ResumeMapper;
import com.blog.blog.service.ResumeService;
import com.blog.blog.service.oss.OssTrackingService;
import com.blog.blog.vo.ResumeDetailVO;
import org.springframework.stereotype.Service;

@Service
public class ResumeServiceImpl implements ResumeService {

	private final ResumeMapper resumeMapper;
	private final OssTrackingService ossTrackingService;

	public ResumeServiceImpl(ResumeMapper resumeMapper, OssTrackingService ossTrackingService) {
		this.resumeMapper = resumeMapper;
		this.ossTrackingService = ossTrackingService;
	}

	@Override
	public ResumeDetailVO getResume() {
		ResumeDetailVO vo = resumeMapper.selectResume();
		if (vo == null) return null;
		vo.setExperiences(resumeMapper.listExperiences(vo.getId()));
		vo.setSkills(resumeMapper.listSkills(vo.getId()));
		vo.setProjects(resumeMapper.listProjects(vo.getId()));
		vo.setEducations(resumeMapper.listEducations(vo.getId()));
		return vo;
	}

	@Override
	public ResumeDetailVO getResumeByUserId(Long userId) {
		if (userId == null) return null;
		ResumeDetailVO vo = resumeMapper.selectResumeByUserId(userId);
		if (vo == null) return null;
		vo.setExperiences(resumeMapper.listExperiences(vo.getId()));
		vo.setSkills(resumeMapper.listSkills(vo.getId()));
		vo.setProjects(resumeMapper.listProjects(vo.getId()));
		vo.setEducations(resumeMapper.listEducations(vo.getId()));
		return vo;
	}

	@Override
	public boolean upsertResume(Long userId, com.blog.blog.dto.SaveResumeDTO dto) {
		if (userId == null || dto == null) return false;
		Long resumeId = resumeMapper.selectResumeIdByUserId(userId);
		String oldAvatar = null;
		if (resumeId != null) {
			ResumeDetailVO old = resumeMapper.selectResumeByUserId(userId);
			oldAvatar = old == null ? null : old.getAvatar();
		}
		int changed;
		if (resumeId == null) {
			changed = resumeMapper.insertResume(userId, dto.getName(), dto.getTitle(), dto.getSummary(), dto.getAvatar(), dto.getEmail(), dto.getPhone(), dto.getLocation(), dto.getGithub(), dto.getLinkedin(), dto.getWebsite());
		} else {
			changed = resumeMapper.updateResume(userId, dto.getName(), dto.getTitle(), dto.getSummary(), dto.getAvatar(), dto.getEmail(), dto.getPhone(), dto.getLocation(), dto.getGithub(), dto.getLinkedin(), dto.getWebsite());
		}
		boolean ok = changed > 0;
		if (ok && oldAvatar != null && dto.getAvatar() != null && !oldAvatar.equals(dto.getAvatar())) {
			ossTrackingService.markUnusedByUrl(oldAvatar);
		}
		if (ok && dto.getAvatar() != null) {
			ossTrackingService.markUsedByUrl(dto.getAvatar());
		}
		return ok;
	}

	@Override
	public Long addExperience(Long userId, com.blog.blog.vo.WorkExperienceVO vo) {
		Long resumeId = resumeMapper.selectResumeIdByUserId(userId);
		if (resumeId == null) return null;
		resumeMapper.insertExperience(resumeId, vo.getCompany(), vo.getPosition(), vo.getStartDate(), vo.getEndDate(), vo.getCurrent(), vo.getDescription(), vo.getAchievements(), vo.getSortOrder());
		return 1L;
	}

	@Override
	public boolean updateExperience(Long userId, com.blog.blog.vo.WorkExperienceVO vo) {
		if (vo == null || vo.getId() == null) return false;
		return resumeMapper.updateExperienceOwned(userId, vo.getId(), vo.getCompany(), vo.getPosition(), vo.getStartDate(), vo.getEndDate(), vo.getCurrent(), vo.getDescription(), vo.getAchievements(), vo.getSortOrder()) > 0;
	}

	@Override
	public boolean deleteExperience(Long userId, Long id) {
		return resumeMapper.deleteExperienceOwned(userId, id) > 0;
	}

	@Override
	public Long addSkill(Long userId, com.blog.blog.vo.SkillVO vo) {
		Long resumeId = resumeMapper.selectResumeIdByUserId(userId);
		if (resumeId == null) return null;
		resumeMapper.insertSkill(resumeId, vo.getName(), vo.getCategory(), vo.getLevel(), vo.getDescription(), vo.getSortOrder());
		return 1L;
	}

	@Override
	public boolean updateSkill(Long userId, com.blog.blog.vo.SkillVO vo) {
		if (vo == null || vo.getId() == null) return false;
		return resumeMapper.updateSkillOwned(userId, vo.getId(), vo.getName(), vo.getCategory(), vo.getLevel(), vo.getDescription(), vo.getSortOrder()) > 0;
	}

	@Override
	public boolean deleteSkill(Long userId, Long id) {
		return resumeMapper.deleteSkillOwned(userId, id) > 0;
	}

	@Override
	public Long addProject(Long userId, com.blog.blog.vo.ProjectVO vo) {
		Long resumeId = resumeMapper.selectResumeIdByUserId(userId);
		if (resumeId == null) return null;
		resumeMapper.insertProject(resumeId, vo.getName(), vo.getDescription(), vo.getHighlights(), vo.getTechnologies(), vo.getGithubUrl(), vo.getDemoUrl(), vo.getImageUrl(), vo.getStartDate(), vo.getEndDate(), vo.getSortOrder());
		if (vo.getImageUrl() != null) {
			ossTrackingService.markUsedByUrl(vo.getImageUrl());
		}
		return 1L;
	}

	@Override
	public boolean updateProject(Long userId, com.blog.blog.vo.ProjectVO vo) {
		if (vo == null || vo.getId() == null) return false;
		ResumeDetailVO cur = resumeMapper.selectResumeByUserId(userId);
		String oldImage = null;
		if (cur != null && cur.getProjects() != null) {
			for (com.blog.blog.vo.ProjectVO p : cur.getProjects()) {
				if (p.getId().equals(vo.getId())) { oldImage = p.getImageUrl(); break; }
			}
		}
		boolean ok = resumeMapper.updateProjectOwned(userId, vo.getId(), vo.getName(), vo.getDescription(), vo.getHighlights(), vo.getTechnologies(), vo.getGithubUrl(), vo.getDemoUrl(), vo.getImageUrl(), vo.getStartDate(), vo.getEndDate(), vo.getSortOrder()) > 0;
		if (ok && oldImage != null && vo.getImageUrl() != null && !oldImage.equals(vo.getImageUrl())) {
			ossTrackingService.markUnusedByUrl(oldImage);
		}
		if (ok && vo.getImageUrl() != null) {
			ossTrackingService.markUsedByUrl(vo.getImageUrl());
		}
		return ok;
	}

	@Override
	public boolean deleteProject(Long userId, Long id) {
		return resumeMapper.deleteProjectOwned(userId, id) > 0;
	}

	@Override
	public Long addEducation(Long userId, com.blog.blog.vo.EducationVO vo) {
		Long resumeId = resumeMapper.selectResumeIdByUserId(userId);
		if (resumeId == null) return null;
		resumeMapper.insertEducation(resumeId, vo.getSchool(), vo.getMajor(), vo.getDegree(), vo.getStartDate() != null ? vo.getStartDate().toString() : null, vo.getEndDate() != null ? vo.getEndDate().toString() : null, vo.getCurrent() ? 1 : 0, vo.getGpa(), vo.getDescription(), vo.getAchievements(), vo.getSortOrder());
		return 1L;
	}

	@Override
	public boolean updateEducation(Long userId, com.blog.blog.vo.EducationVO vo) {
		if (vo == null || vo.getId() == null) return false;
		return resumeMapper.updateEducationOwned(userId, vo.getId(), vo.getSchool(), vo.getMajor(), vo.getDegree(), vo.getStartDate() != null ? vo.getStartDate().toString() : null, vo.getEndDate() != null ? vo.getEndDate().toString() : null, vo.getCurrent() ? 1 : 0, vo.getGpa(), vo.getDescription(), vo.getAchievements(), vo.getSortOrder()) > 0;
	}

	@Override
	public boolean deleteEducation(Long userId, Long id) {
		return resumeMapper.deleteEducationOwned(userId, id) > 0;
	}
}


